<?php

/**
 * Class MapDiad.php
 *
 * Class responsible of store the map of each group
 *
 * @author Jose Oliveira <jafo@ua.pt> 2012
 */

class MapDiad{


	private $groupID;
	private $projectID;
	private $jsonString;


	/**
	 * 
	 * @param integer $groupID
	 * @param integer $projectID
	 * @param string $jsonString
	 */
	function __construct($groupID, $projectID, $jsonString){

		if(isset($groupID) && isset($projectID) && isset($jsonString) && $jsonString != ""){
			$this->groupID=$groupID;
			$this->projectID=$projectID;
			$this->jsonString=$jsonString;
		}

	}

	/**
	 * Method that based on private methods 'isNew()', 'saveNewStage()' and 'updateStage()', 
	 * decide which method to use to record the data of the ArguQuest map
	 * @return bool
	 */ 
	public function saveStage(){

		if($this->isNew()){
			$this->saveNewStage();
			return true;
		}else{
			$this->updateStage();
			return true;
		}
		
		
		return false;
	}

	/**
	 * Method private which stores a map in the database, case the group is saving the map for the first time
	 * @throws Exception
	 * @return bool
	 */
	private function saveNewStage(){

		$conn=new ConnDBUser();
		$conn->connect();
		
		$json=mysql_real_escape_string($this->jsonString);

		$result=mysql_query("insert into mapDiad(jsonString, groupID, projectID) values('$json', '" . $this->groupID . "', '" . $this->projectID . "')");

		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro ao guardar o mapa ArguQuest");
		}

		try{
			$conn->disconnect($result);
			return true;
		}catch(Exception $e){
			$e->getMessage();
		}

	}

	/**
	 * Method private which stores a map in the database, case the group
	 * have already saved the map at least one time
	 * @throws Exception
	 * @return bool
	 */
	private function updateStage(){

		$conn=new ConnDBUser();
		$conn->connect();
		
		$json=mysql_real_escape_string($this->jsonString);

		$result=mysql_query("update mapDiad set jsonString='$json' where groupID='" . $this->groupID . "' and projectID='" . $this->projectID . "'");

		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro ao guardar o mapa ArguQuest");
		}

		try{
			$conn->disconnect($result);
			return true;
		}catch(Exception $e){
			$e->getMessage();
		}
	}

	/**
	 * Method that checks if a map is beeing saved for the first time or not
	 * @throws Exception
	 * @return boolean 
	 * 
	 * return true if is a new map, false otherwise
	 */
	private function isNew(){
		/*
		 * Retona true se o mapa for novo, false se o mapa já tiver sido armazenado pelo menos uma vez
		 */

		$conn=new ConnDBUser();
		$conn->connect();

		$result=mysql_query("select mapID from mapDiad where groupID='" . $this->groupID . "' and projectID='" . $this->projectID . "'");

		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro ao guardar o mapa");
		}

		try{
			$num=mysql_num_rows($result);
			$conn->disconnect($result);
				
			if($num==0){
				return true;
			}else{
				return false;
			}
				
		}catch(Exception $e){
			$e->getMessage();
		}

	}

	/**
	 * Method used to delete a map stored by a group; useful for when you want to go back to retrieve the automatic map generated by the system, 
	 * based on associations made ​​by the user
	 * @param integer $groupID
	 * @param integer $projectID
	 * @throws Exception
	 */
	
	public function resetMap($groupID, $projectID){

		$conn=new ConnDBAdmin();
		$conn->connect();

		$result=mysql_query("delete from mapDiad where groupID=$groupID and projectID=$projectID");

		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro ao carregar o mapa");
		}

		try{
			$conn->disconnect($result);
			return true;
		}catch(Exception $e){
			$e->getMessage();
		}
	}

	/**
	 * Method that verifies if a certain map is beeing stored for the first time
	 * @param integer $groupID
	 * @param integer $projectID
	 * @throws Exception
	 */
	public function mapExists($groupID, $projectID){
		/*
		 * Retona true se o mapa existir, false se o mapa não existir
		 */

		$conn=new ConnDBUser();
		$conn->connect();

		$result=mysql_query("select mapID from mapDiad where groupID=$groupID and projectID=$projectID");

		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro ao guardar o mapa");
		}

		try{
			$num=mysql_num_rows($result);
			$conn->disconnect($result);
				
			if($num==0){
				return false;
			}else{
				return true;
			}
				
		}catch(Exception $e){
			$e->getMessage();
		}

	}

	/**
	 * Method that getting a groupID and ProjectID, returns the respective JSON string with the map information 
	 * @param integer $projectID
	 * @param integer $groupID
	 * @throws Exception
	 */
	public function getJsonString($projectID, $groupID){
		
		$conn=new ConnDBUser();
		$conn->connect();

		$result=mysql_query("select jsonString from mapDiad where projectID='$projectID' and groupID='$groupID'");

		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro ao carregar o mapa ArguQuest");
		}

		try{
			$registo=mysql_fetch_array($result);
			$conn->disconnect($result);
			$string=$registo['jsonString'];
				
			if($string!=""){
				return $string;
			}else{
				return 0;
			}
				
		}catch(Exception $e){
			$e->getMessage();
		}
	}

	/**
	 * This method is used ehen a map image is saved in the server (location: clientResources/images/map/), 
	 * and we save a reference to this path in the database.
	 * @param integer $groupID
	 * @param integer $projectID
	 * @param integer $path
	 * @throws Exception
	 */
	public function saveImgPath($groupID, $projectID, $path){

		$conn=new ConnDBUser();
		$conn->connect();

		$result=mysql_query("update mapDiad set path='$path' where groupID=$groupID and projectID=$projectID");

		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro ao guardar a imagem no disco");
		}

		try{
				
			$conn->disconnect($result);
			return true;
				
		}catch(Exception $e){
			$e->getMessage();
		}

	}

	/**
	 * Method that returns the actual path for a map image, to be used in a PDF for example
	 * @param integer $groupID
	 * @param integer $projectID
	 * @throws Exception
	 */
	public function getActualPath($groupID, $projectID){
		/*
		 * Metodo que retorna o imgPath atual de uma imagem
		 */

		$conn=new ConnDBUser();
		$conn->connect();

		$result=mysql_query("select path from mapDiad where groupID=$groupID and projectID=$projectID");

		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro ao guardar a imagem no disco");
		}

		try{
				
			$registo=mysql_fetch_array($result);
			return $registo['path'];
				
		}catch(Exception $e){
			$e->getMessage();
		}

	}
	
	/**
	 * During the edition of the map by a diad, we need to check if the other elements of our group
	 * is online or not. 
	 * @param integer $mateID
	 * @throws Exception
	 * @return bool
	 */
	public function checkIfMateAlive($mateID){
		
		$conn=new ConnDBUser();
		$conn->connect();
		
		$result=mysql_query("select onID from online where userID=$mateID");
		
		if(!$result){
			throw new Exception("Lamentamos mas ocorreu um erro inesperado");
		}
		
		try{
			
			$num=mysql_num_rows($result);
			$conn->disconnect($result);
			
			if($num>0){
				return true;
			}else{
				return false;
			}
			
		}catch(Exception $e){
			$e->getMessage();
		}
	}

}